#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File        :   202.快乐数.py    
@Contact     :   lhouctcs@163.com
@License     :   MIT
@Time        :   2023/2/6 12:22
@Author      :   lhou 
@Version     :   0.1.0
@Description :
"""


# import lib
class Solution:
    def isHappy(self, n: int) -> bool:
        s = set()

        while True:
            sum = self.sum(n)
            if sum in s:
                return False
            if sum == 1:
                return True
            n = sum
            s.add(n)

    def sum(self, n):
        s = 0
        while n:
            s += (n % 10) ** 2
            n //= 10
        return s
